/*
    Copyright 2009 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package ot;

import java.io.*;

/** Base class for implementations of {@link Ot}.
 *
 * @author Barnet Wagman
 */
abstract public class AbstractOt implements Ot, Serializable {

    static final long serialVersionUID = ConstantsOt.UID_VERSION;
   
    protected OtableCache readCache;

    protected SerializerTable sert;

        /** This constuctor creates a list of serializers that contains
         *  {@link StandardSerializer}, {@link GzipSerializer}, and the
         *  default serializer (if it's not the standard or gzip).         
         *  <tt>cacheSize=</tt>{@link ConstantsOt#MAX_OBJ_IN_CACHE},
         *  <tt>cacheDecrement=</tt>{@link ConstantsOt#CACHE_DECREMENT}.
         *
         * @param defaultSerializer
         */
    public AbstractOt(Serializer defaultSerializer) {
        sert = new SerializerTable(defaultSerializer);
        readCache = new OtableCache(ConstantsOt.MAX_OBJ_IN_CACHE,
                                    ConstantsOt.CACHE_DECREMENT);
    }

        /** This constuctor creates a list of serializers that contains
         *  {@link StandardSerializer}, {@link GzipSerializer} and the
         *  default serializer (if it's not the standard or gzip).
         *
         * @param defaultSerializer
         * @param cacheSize maximum number of objects in the cache.
         * @param cacheDecrement when the cache reaches it limit (<tt>cacheSize</tt>),
         *      it is shrunk by this value.
         */
    public AbstractOt(Serializer defaultSerializer,
                      int cacheSize, int cacheDecrement) {
        sert = new SerializerTable(defaultSerializer);
        readCache = new OtableCache(cacheSize,
                                    cacheDecrement);
    }

        /** This constuctor creates a list of serializers that contains
         *  {@link StandardSerializer} and {@link GzipSerializer}.  The
         *  default serializer is set to {@link GzipSerializer}.
         *
         * @param defaultSerializer
         * @param cacheSize maximum number of objects in the cache.
         * @param cacheDecrement when the cache reaches it limit (<tt>cacheSize</tt>),
         *      it is shrunk by this value.
         */
    public AbstractOt(int cacheSize, int cacheDecrement) {
        this(new GzipSerializer(),cacheSize,cacheDecrement);
    }

        /** This constuctor creates a list of serializers that contains
         *  {@link StandardSerializer} and {@link GzipSerializer}.  The
         *  default serializer is set to {@link GzipSerializer}.
         *  <tt>cacheSize=</tt>{@link ConstantsOt#MAX_OBJ_IN_CACHE},
         *  <tt>cacheDecrement=</tt>{@link ConstantsOt#CACHE_DECREMENT}.
         *
         */
    public AbstractOt() {
        this(new GzipSerializer());
    }

    public void addSerializer(Serializer s) {
        sert.addSerializer(s);
    }

    public Serializer getDefaultSerializer() {
        return(sert.getDefaultSerializer());
    }

    protected Serializer getSer(String name) {
        return(sert.getSerializer(name));
    }

    protected Serializer getSer(Path path) {
        return(sert.getSerializer(path.toPathString()));
    }

    protected Serializer getSer(LocO loc) {
        return(sert.getSerializer(loc.getPath().toPathString()));
    }
}
